package com.aletto.general_store.presenters;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.entities.WalletAccount;
import com.aletto.general_store.data.model.UserDetails;
import com.aletto.general_store.ui.activities.WalletAccountActivity;
import com.aletto.general_store.data.ERC20ServiceAbi;
import com.aletto.general_store.utils.LocalConfigStore;
import com.aletto.general_store.utils.image.GlideApp;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.utils.RxUtil;
import com.sisyphus.spinach.ui.configs.impl.DefaultTemplate;

import org.web3j.crypto.Credentials;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class UserDetailsTemplate extends DefaultTemplate<UserDetails> {

    private final View mLoginButton, mUserProfileLayout, mWalletNameTextView;
    private final TextView tvBNB, tvUSDT;

    public UserDetailsTemplate(ViewGroup parent) {
        super(parent);
        mWalletNameTextView = parent.findViewById(R.id.tv_wallet_name);
        mLoginButton = mParent.findViewById(R.id.button_login);
        mUserProfileLayout = mParent.findViewById(R.id.user_profile_layout);

        tvBNB = mParent.findViewById(R.id.text_view_BNB);
        tvUSDT = mParent.findViewById(R.id.text_view_USDT);
    }

    @Override
    public INetTask.View<UserDetails> view() {
        return new INetTask.View<>() {
            @Override
            public void success(UserDetails data) {
                LocalConfigStore.getInstance().saveID(data.getId());
                LocalConfigStore.getInstance().saveToken(data.getUser_token());
                LocalConfigStore.getInstance().saveContractAddress(data.getAddress());

                mLoginButton.setVisibility(View.GONE);
                mUserProfileLayout.setVisibility(View.VISIBLE);
                mWalletNameTextView.setOnClickListener(v ->
                        v.getContext().startActivity(new Intent(v.getContext(), WalletAccountActivity.class)));

                TextView tv_signature = mParent.findViewById(R.id.tv_signature);
                TextView tv_wallet_name = mParent.findViewById(R.id.tv_wallet_name);
                ImageView iv_avatar = mParent.findViewById(R.id.iv_avatar);

                String sign = data.getSign();
                if (sign == null || sign.isEmpty()) {
                    tv_signature.setText(R.string.not_set_sign_tips);
                } else {
                    tv_signature.setText(sign);
                }

                String name = data.getName();
                if (name == null || name.isEmpty()) {
                    tv_wallet_name.setText(R.string.not_set_sign_tips);

                } else {
                    tv_wallet_name.setText(name);
                }

                GlideApp.with(iv_avatar).load(data.getHead_img()).avatar().into(iv_avatar);

                WalletAccount wallet = LocalConfigStore.getInstance().getWallet();
                ERC20ServiceAbi.balance(wallet.getAddress())
                        .compose(RxUtil.transformer())
                        .subscribe(new Observer<>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NonNull BigDecimal bigDecimal) {
                                tvBNB.setText(new DecimalFormat("0.0000").format(bigDecimal) + " BNB");
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });


                ERC20ServiceAbi.balance(
                                Credentials.create(wallet.getPrivateKey()), ERC20ServiceAbi.USDT_CONTRACT_ADDRESS)
                        .compose(RxUtil.transformer())
                        .subscribe(new Observer<BigDecimal>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NonNull BigDecimal bigDecimal) {
                                tvUSDT.setText(new DecimalFormat("0.00").format(bigDecimal) + " USDT");
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        };
    }

    @Override
    public INetTask.Presenter<UserDetails, INetTask.View<UserDetails>> presenter() {
        WalletAccount wallet = LocalConfigStore.getInstance().getWallet();
        String address = wallet == null ? "" : wallet.getAddress();

        if (address == null) {
            mLoginButton.setVisibility(View.VISIBLE);
            mUserProfileLayout.setVisibility(View.INVISIBLE);
        }

        return new NetSingleTask<>(DataRepository.getInstance().login(address));
    }

}
